// Copyright 2022 Huawei Cloud Computing Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "CaeParamStorage.h"
#include <utility>
#include <vector>
#include "../cae_common/CaeLogConfig.h"
#include "../cae_common/CaeCommon.h"
#include "../cae_common/CaeAppCtrlCmdUtils.h"

static const std::string CLIENT_PLATFORM_TYPE_WINDOWS = "windows";
static const std::string STREAM_OPERATE_TYPE = "stream";
static const std::string SCREENSHOT_OPERATE_TYPE = "screenshot";

CaeParamStorage::CaeParamStorage()
{
    InitClientParams();
    InitSessionParams();
}

void CaeParamStorage::InitClientParams() noexcept
{
    m_ip = "";
    m_port = "";
    m_ticket = "";
    m_authTs = "";
    m_verifyData = "";
    m_encryptedData = "";
    m_aesIv = "";
    m_sdkVersion = "";
    m_protocolVersion = "";
    m_sessionId = "";
    m_backgroundTimeout = "";
    m_availablePlaytime = "";
    m_userId = "";
    m_touchTimeout = "";
    m_clientPlatformType = "";
}

void CaeParamStorage::InitSessionParams() noexcept
{
    m_startTime = 0;
    m_aesKey = "";
}

CaeParamStorage::~CaeParamStorage()
{
    ClearAllParams();
}

void CaeParamStorage::ClearAllParams() noexcept
{
    InitClientParams();
    InitSessionParams();
}

void CaeParamStorage::SetIp(std::string ip)
{
    m_ip = std::move(ip);
}

std::string CaeParamStorage::GetIp()
{
    return m_ip;
}

void CaeParamStorage::SetPort(std::string port)
{
    m_port = std::move(port);
}

std::string CaeParamStorage::GetPort()
{
    return m_port;
}

void CaeParamStorage::SetUserId(std::string userId)
{
    m_userId = std::move(userId);
}

std::string CaeParamStorage::GetUserId()
{
    return m_userId;
}

void CaeParamStorage::SetTicket(std::string ticket)
{
    m_ticket = std::move(ticket);
}

std::string CaeParamStorage::GetTicket()
{
    return m_ticket;
}

void CaeParamStorage::SetAuthTs(std::string authTs)
{
    m_authTs = std::move(authTs);
}

std::string CaeParamStorage::GetAuthTs()
{
    return m_authTs;
}

void CaeParamStorage::SetVerifyData(std::string verifyData)
{
    m_verifyData = std::move(verifyData);
}

std::string CaeParamStorage::GetVerifyData()
{
    return m_verifyData;
}

void CaeParamStorage::SetEncyptedData(std::string encyptedData)
{
    m_encryptedData = std::move(encyptedData);
}

std::string CaeParamStorage::GetEncyptedData()
{
    return m_encryptedData;
}

void CaeParamStorage::SetAesIv(std::string aesIv)
{
    m_aesIv = std::move(aesIv);
}

std::string CaeParamStorage::GetAesIv()
{
    return m_aesIv;
}

void CaeParamStorage::SetSdkVersion(std::string sdkVersion)
{
    m_sdkVersion = std::move(sdkVersion);
}

std::string CaeParamStorage::GetSdkVersion()
{
    return m_sdkVersion;
}

void CaeParamStorage::SetProtolcolVersion(std::string protocolVersion)
{
    m_protocolVersion = std::move(protocolVersion);
}

std::string CaeParamStorage::GetProtocolVersion()
{
    return m_protocolVersion;
}

void CaeParamStorage::SetSessionId(std::string sessionId)
{
    m_sessionId = std::move(sessionId);
}

std::string CaeParamStorage::GetSessionId()
{
    return m_sessionId;
}

void CaeParamStorage::SetBackgroundTimeout(std::string timeout)
{
    m_backgroundTimeout = std::move(timeout);
}

std::string CaeParamStorage::GetBackgroundTimeout()
{
    return m_backgroundTimeout;
}

void CaeParamStorage::SetAvailablePlayTime(std::string availablePlayTime)
{
    m_availablePlaytime = std::move(availablePlayTime);
}

std::string CaeParamStorage::GetAvailablePlayTime()
{
    return m_availablePlaytime;
}

void CaeParamStorage::SetAesKey(std::string aeskey)
{
    m_aesKey = std::move(aeskey);
}

std::string CaeParamStorage::GetAeskey()
{
    return m_aesKey;
}

void CaeParamStorage::SetStartTime(time_t startTime)
{
    LOGI("setStartTime:%ld", startTime);
    this->m_startTime = startTime;
};

time_t CaeParamStorage::GetStartTime()
{
    if (this->m_startTime == 0) {
        LOGE("startTime is error!!!");
        return 0;
    }
    return this->m_startTime;
}

void CaeParamStorage::SetTouchTimeout(std::string touchTimeout)
{
    m_touchTimeout = touchTimeout;
}

std::string CaeParamStorage::GetTouchTimeout()
{
    return m_touchTimeout;
}

void CaeParamStorage::SetMaxDisconnectDuration(std::string &disconnectTimes)
{
    m_maxDisconnectDuration = disconnectTimes;
}

std::string CaeParamStorage::GetMaxDisconnectDuration()
{
    return m_maxDisconnectDuration;
}

void CaeParamStorage::SetClientPlatformType(std::string clientPlatformType)
{
    m_clientPlatformType = clientPlatformType == CLIENT_PLATFORM_TYPE_WINDOWS ?
                           std::to_string(PLATFORM_WINDOWS) : clientPlatformType;
}

std::string CaeParamStorage::GetClientPlatformType()
{
    return m_clientPlatformType;
}

void CaeParamStorage::SetRegionId(std::string regionId)
{
    m_regionId = regionId;
}

std::string CaeParamStorage::GetRegionId()
{
    return m_regionId;
}

void CaeParamStorage::SetClientMode(std::string clientMode)
{
    m_clientMode = clientMode;
}

std::string CaeParamStorage::GetClientMode()
{
    return m_clientMode;
}

std::string CaeParamStorage::GetPublicCrtFilePath()
{
    if (m_clientMode.empty()) {
        LOGE("client mode is null");
        return "";
    }
    return m_clientMode == CLIENT_MODE_MANAGEMENT ? CaeConfigManage::GetManagementPublicCrtFilePath() : CaeConfigManage::GetDirectPublicCrtFilePath();
}

std::string CaeParamStorage::GetObtainPublicCrtUrl(std::string regionId)
{
    std::string configUrl = CaeConfigManage::GetManagementPublicCrtObtainUrl();
    if (!configUrl.empty()) {
        return configUrl;
    }
    char url[1024] = {0};
    if (regionId.empty()) {
        LOGE("Region id is null");
        return "";
    }
    if(snprintf_s(url, sizeof(url), sizeof(url) - 1
            , CaeConfigManage::GetDirectPublicCrtObtainUrlFormat().c_str(), regionId.c_str(), regionId.c_str()) == -1) {
        LOGE("Failed to get url.");
        return "";
    }
    return url;
}

void CaeParamStorage::SetIsScreenshot(const std::string& operateType) {
    if (!(operateType.empty()) && operateType == SCREENSHOT_OPERATE_TYPE) {
        m_isScreenshot = true;
    } else {
        m_isScreenshot = false;
    }
}

bool CaeParamStorage::IsScreenshot() const {
    return m_isScreenshot;
}

void CaeParamStorage::SetJpegWidth(const char* streamWidth) {
    m_jpegWidth = CaeCommon::StrToUInt32(KEY_STREAM_WIDTH.c_str(),
                                         streamWidth,
                                         m_jpegWidth);
}

void CaeParamStorage::SetJpegHeight(const char* streamHeight) {
    m_jpegHeight = CaeCommon::StrToUInt32(KEY_STREAM_HEIGHT.c_str(),
                                          streamHeight,
                                          m_jpegHeight);
}

uint32_t CaeParamStorage::GetJpegWidth() const {
    return m_jpegWidth;
}

uint32_t CaeParamStorage::GetJpegHeight() const {
    return m_jpegHeight;
}